<?php
$v_worker_id = $_SESSION['staff_id'];

if(isset($_REQUEST['fuseaction_back'])) {
	$fuseaction_back=$_REQUEST['fuseaction_back'];
}else{
	$fuseaction_back = "";
}

$v_reason = "";
if(isset($_REQUEST['txt_reason'])){
	$v_reason = _replace_bad_char(trim($_REQUEST['txt_reason']));
} 
$v_recordtype_filter = $_SESSION['onegate_current_recordtype'];
$v_record_id_list = "";
if(isset($_REQUEST['hdn_record_id_list'])){
	$v_record_id_list = $_REQUEST['hdn_record_id_list'];
	$arr_record_id_list = split(",",$v_record_id_list);
}

#print_r($arr_record_id_list); echo "<br>********<br>";

$v_work = "";
if(isset($_REQUEST['hdn_work'])){
	$v_work = $_REQUEST['hdn_work'];
}
#echo "v_work: ", $v_work; exit;
$v_question = "";
if(isset($_REQUEST['hdn_question'])){
	$v_question = $_REQUEST['hdn_question'];
}
$v_work_date = "";
if(isset($_REQUEST['txt_work_date'])){
	$v_work_date= $_REQUEST['txt_work_date'];
} 

$v_handler_id = "";
if(isset($_REQUEST['hdn_handler_id'])){
	$v_handler_id = $_REQUEST['hdn_handler_id'];
}
//echo 'v_handler_id'.$v_handler_id.'<br>'; 
//echo 'v_worker_id'.$v_worker_id.'<br>'; 
$v_reason_refuse_header = "";
if(isset($_REQUEST['hdn_reason'])){
	$v_reason_refuse_header = _replace_bad_char(trim($_REQUEST['hdn_reason']));
} 

// Update by Dinh, Nguyen
// NSD selected option LD_PHONG_CAPPHEP cho HS da tu choi, thi gan lai la duoc cap phep
$v_rad_status = "";
if(isset($_REQUEST['rad_status'])){
	$v_rad_status = _replace_bad_char(trim($_REQUEST['rad_status']));
} 
// echo $v_rad_status, " ^^^^ <br>"; 

//Xac dinh xem can bo thu ly co ky duyet thay Lanh dao khong
if(isset($_REQUEST['hdn_approve_by_handler'])) {
	$v_approve_by_handler=$_REQUEST['hdn_approve_by_handler'];
}else{
	$v_approve_by_handler = "";
}
if ($v_approve_by_handler) {
//Xac dinh loai ho so nay ai phe duyet cap phong, ai phe duyet cap co quan	
	$v_arr_all_recordtype = $_SESSION['recordtype'];
	for($i=0; $i< sizeof($v_arr_all_recordtype); $i++){
		if ($v_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $v_recordtype_filter ){
			$v_handle_leader = $v_arr_all_recordtype[$i]['FK_HANDLE_LEADER'];
			$v_approve_leader = $v_arr_all_recordtype[$i]['FK_APPROVE_LEADER'];
		}
	}

	if (!is_null($v_approve_leader) && !is_null($v_handle_leader) && ($v_approve_leader != $v_handle_leader)){
		$v_worker_id = $v_approve_leader;
		if ($v_work == "LD_PHONG_CAPPHEP"){
			$v_work = "LD_COQUAN_CAPPHEP";
			if(trim($v_reason)<>"")
				$v_reason = "";
		}elseif ($v_work == "LD_PHONG_TUCHOI"){
			$v_work = "LD_COQUAN_TUCHOI";
		}
	}else
		$v_worker_id = $v_handle_leader;
		$fuseaction_back = "DISPLAY_ALL_RECORD_FOR_APPROVING";
}
/*
	Doan ma nay kiem tra xem la nguoi thu ly cap nhat ho trang thay ho so cho lanh dao lay la trong 
	qua trinh thu ly trinh lanh dao ky bi lanh dao tu choi can bo thu ly gui tra lai co bo phan tiep nhan tra
	lai cho cong dan
*/

/*
echo "<br> begin: ",$v_handler_id, " --- ", $_SESSION['staff_id'], " ++ ", $v_reason_refuse_header, " *** ", $v_reason ,"<br>";
echo 'v_work:'.$v_work.'<br>';
*/

if($v_work == 'TRINH_LD_PHONG_LAN_DAU'){
	$v_status_refuse = 0;
	$v_work == 'TRINH_LD_PHONG_LAN_DAU';
}else{
	if(($v_work == 'TRINH_LD_PHONG') && (($v_reason=="") || (is_null($v_reason))) && (sizeof($arr_record_id_list) <= 1)){
		$v_status_refuse = 0;	//$v_work = "TRINH_LD_PHONG";		
	}else{
		if(($v_handler_id == $_SESSION['staff_id']) && ((trim($v_reason_refuse_header)<>"") || (trim($v_reason)<>""))){
			$v_status_refuse = 1;
			$v_work = "TRA_LAI_HOSO";	
		}
	}	
}	
#echo "sau khi gan lai lan 1: ", $v_work, "<br>";
if(($v_work == 'LD_PHONG_TUCHOI') && (sizeof($arr_record_id_list) >= 1)){
#if(($v_work == 'LD_PHONG_TUCHOI') && (sizeof($arr_record_id_list) > 1)){
		$v_status_refuse = 1;
		$v_work = "TRA_LAI_HOSO";	
}

/**************************************************************************
* Update by Dinh, Nguyen - 28062007
* Neu NSD chon Duoc cap phep thi gan lai trang thai la: LD_PHONG_CAPPHEP
**************************************************************************/
if($v_rad_status == 'LD_PHONG_CAPPHEP'){
	$v_work = "LD_PHONG_CAPPHEP";
	if(trim($v_reason)<>"")
		$v_reason = "";	
}
// End Dinh, Nguyen

//echo 'v_reason_refuse_header:'.$v_reason_refuse_header.'<br>';
#echo 'v_reason:'.$v_reason.'<br>'; exit;
if(_is_sqlserver()){

	$sql = "Exec Onegate_RecordWorkSystemUpdate " ;
	$sql = $sql . "'" . $v_record_id_list . "'";
	$sql = $sql . "," . $v_worker_id;
	$sql = $sql . ",'" . $v_work . "'";
	$sql = $sql . ",'" . _ddmmyyyy_to_yyyymmdd($v_work_date) . "'" ;
	$sql = $sql . "," . $_ISA_AUTOMATIC_UPDATE_WORK_LIST;
	$sql = $sql . ",'" . $v_reason . "'" ;
	if(($v_status_refuse == 1) && ($v_reason<>"")){
		$sql = $sql . "," . $v_status_refuse;
	}else{
		$v_status_refuse = 0;
		$sql = $sql . "," . $v_status_refuse;
	}	
	//echo $sql .'<br>'; exit;
	$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
	$rs = $ado_conn->GetRow($sql); 
	$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
}
sleep(0);
if (!is_null($v_error) && $v_error<>""){?>
	<script>
		alert("<?php echo $v_error; ?>");
		if (_MODAL_DIALOG_MODE==1){
			window.location = "<?php echo $v_goto_url; ?>";
		}else{
			window.history.back();
		}	
	</script><?php
	exit;
}
?>
<form action="index.php" method="post" name="f_back">
	<input type="hidden" name="fuseaction" value="<?php echo $fuseaction_back; ?>">
	<input type="hidden" name="fuseaction_back" value="<?php echo $fuseaction_back; ?>">
	<input type="hidden" name="hdn_recordtype_filter" value="<?php echo $v_recordtype_filter;?>">
	<input type="hidden" name="hdn_record_id" value="<?php echo $v_record_id_list;?>">
	<input type="hidden" name="hdn_work" value="<?php echo $v_work;?>">
	<input type="hidden" name="hdn_question" value="<?php echo $v_question;?>">
</form>
<Script language="javascript">
	if (document.forms(0).hdn_question.value=="OK" && document.forms(0).hdn_work.value=="LD_PHONG_CAPPHEP"){
		if (confirm("Ban co muon cap nhat noi dung giay phep")){
			document.forms(0).fuseaction.value = "DISPLAY_SINGLE_LICENSE";
		}
	}
	document.forms(0).submit();
</Script>
